//
//  AboutUsViewController.m
//  reacticons
//
//  Created by Nong Trung Nghia on 9/18/12.
//
//

#import "AboutUsViewController.h"
#define SITE_LINK_BTN 1
#define EMAIL_BTN 2
@interface AboutUsViewController ()

@end

@implementation AboutUsViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        self.view.backgroundColor=[UIColor blackColor];
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.title=@"About Us";
    [_webview.scrollView setAlwaysBounceHorizontal:NO];
    [_webview.scrollView setAlwaysBounceVertical:NO];
    [_webview.scrollView setBounces:NO];
    [_webview setHidden:YES];
//    [_webview loadRequest:[NSURLRequest requestWithURL:[[NSBundle mainBundle] URLForResource:@"help" withExtension:@"htm"]]];

    // Do any additional setup after loading the view from its nib.
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

-(IBAction)clickedButton:(id)sender{
    switch ([sender tag]) {
        case SITE_LINK_BTN:
            [self showWeb];

            break;
        case EMAIL_BTN:
            [self sendMailTo:@"reacticons@gmail.com" WithSubject:nil andBody:@"<br/>(if submitting a pic, remember to paste it in this email!)" andLinkURL:nil];

            break;
        case 3:
            [_webview setHidden:YES];
            self.navigationItem.rightBarButtonItem=nil;
            break;
        default:
            break;
    }
}

-(void)showWeb{
    [_webview setHidden:NO];
    [_webview setUserInteractionEnabled:YES];
    [_webview loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.reacticons.com"]]];
    UIBarButtonItem *cancelBtn=[[UIBarButtonItem alloc] initWithTitle:@"Cancel" style:UIBarButtonItemStyleDone target:self action:@selector(clickedButton:)];
    [cancelBtn setTag:3];
    self.navigationItem.rightBarButtonItem=cancelBtn;
    [cancelBtn release];

}

#pragma mark - Send Mail

-(void)sendMailTo:(NSString*)_receiver
      WithSubject:(NSString*)_subject
          andBody:(NSString*)_body
       andLinkURL:(NSString*)_url
{
    
    if ([MFMailComposeViewController canSendMail]) {
        
        // Fill out the email body text
        //Create a string with HTML formatting for the email body
        
        MFMailComposeViewController *emailDialog = [[MFMailComposeViewController alloc] init];
        emailDialog.mailComposeDelegate = self;
        if (_subject) {
            [emailDialog setSubject:_subject];
            
        }
        if (_body) {
            NSMutableString *emailBody = [[[NSMutableString alloc] initWithString:@"<html><body>"] retain];
            //Add some text to it however you want
            [emailBody appendString:[NSString stringWithFormat:@"<p>%@</p>",_body]];
            if (_url) {
                [emailBody appendString:[NSString stringWithFormat:@"<a href='%@'>%@</a>",_url,_url]];
            }
            [emailBody appendString:@"</body></html>"];
            [emailDialog setMessageBody:emailBody isHTML:YES];
            [emailBody release];
            
            
        }
        
        if (_receiver) {
            [emailDialog setToRecipients:[NSArray arrayWithObject:_receiver]];
        }
        [self presentModalViewController:emailDialog animated:YES];
        [emailDialog release];
    }
    else {
        
        UIAlertView *alert =[[UIAlertView alloc] initWithTitle:@"Cannot Send Via Mail" message:@"Please setup your mail before share via Email" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles: nil];
        [alert show];
        [alert release];
    }
    
    
    
}
-(void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error {
    
    [self dismissModalViewControllerAnimated:YES];
    
}

@end
